In [1]:
%load_ext autoreload
%autoreload 2
import os
import sys
# Get current Directory
wd = os.getcwd()
# Add package to path
#sys.path.append(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)), 'openigtlink'))
sys.path.append(os.path.abspath(os.path.join(wd, os.pardir)))

In [4]:
from openigtlink import OpenIGTLink
import numpy

In [5]:
header = OpenIGTLink.OpenIGTLinkHeader()
header.setTYPE('STATUS')
header.setDEVICE_NAME('Device Name Example')
header.setTIME_STAMP(123,0.456)

bodyExample = 'This is an example of body message'
header.setCRC64(bodyExample) # Need the body to calculate the CRC
header.setBODY_SIZE(len(bodyExample))

print header

messageHeader = header.getHeaderMessage()
headerTest = OpenIGTLink.OpenIGTLinkHeader()
headerTest.unpack(messageHeader)
headerTest.setCRC64(bodyExample)
print headerTest


#### Header 

####        Version             :1
####        Type                :STATUS
####        Device Name         :Device Name Example
####        Time Stamp          :123.456 sec.
####        Body Size           :34 B
####        CRC64               :5019106130826548958
####        Byte Representation :'\x00\x01STATUS\x00\x00\x00\x00\x00\x00Device Name Example\x00\x00\x00\x00{t\xbcj~\x00\x00\x00\x00\x00\x00\x00"E\xa7ro\x01\xe6\xd6\xde'


#### Header 

####        Version             :1
####        Type                :STATUS
####        Device Name         :Device Name Example
####        Time Stamp          :123.456 sec.
####        Body Size           :34 B
####        CRC64               :5019106130826548958
####        Byte Representation :'\x00\x01STATUS\x00\x00\x00\x00\x00\x00Device Name Example\x00\x00\x00\x00{t\xbcj~\x00\x00\x00\x00\x00\x00\x00"E\xa7ro\x01\xe6\xd6\xde'



In [6]:
body = OpenIGTLink.OpenIGTLinkBody()
bodyExample = 'This is an example of body message'
body.setBodyMessage(bodyExample) # Need the body to calculate the CRC
print body


#### Body 

####        Byte Representation :'This is an example of body message'



In [7]:
igtmessage = OpenIGTLink.OpenIGTLinkMessage()
igtmessage.header = OpenIGTLink.OpenIGTLinkHeader()
igtmessage.header.setTYPE('STATUS')
igtmessage.header.setDEVICE_NAME('Device Name Example')
igtmessage.header.setTIME_STAMP(123,0.456)
bodyExample = 'This is an example of body message'
igtmessage.body.setBodyMessage(bodyExample)
bodyCoding = '>' + 'c' * len(bodyExample)
igtmessage.__updateMessage__(bodyCoding)
print igtmessage


########################

#### IGTLink Message

#### Header 

####        Version             :1
####        Type                :STATUS
####        Device Name         :Device Name Example
####        Time Stamp          :123.456 sec.
####        Body Size           :34 B
####        CRC64               :5019106130826548958
####        Byte Representation :'\x00\x01STATUS\x00\x00\x00\x00\x00\x00Device Name Example\x00\x00\x00\x00{t\xbcj~\x00\x00\x00\x00\x00\x00\x00"E\xa7ro\x01\xe6\xd6\xde'

#### Body 

####        Byte Representation :'This is an example of body message'

########################


In [8]:
transform = OpenIGTLink.OpenIGTLinkTransform()

npTransform = numpy.eye(4)
npTransform[0,0] = 2
npTransform[1,1] = 3
npTransform[3,1] = 15

message = transform.setOpenIGTLinkTransform( npTransform = npTransform,\
                                            floatTimeStamp = 123.456,\
                                            transformName = 'RigidToTracker Test')
print transform

transformTest = OpenIGTLink.OpenIGTLinkTransform()
transformTest.header.unpack(message[0:transformTest.header.IGTLinkHeaderSize])
transformTest.unpackTransform(message[transformTest.header.IGTLinkHeaderSize::])
print transformTest


########################

#### Transform Message

####        Transform           :
[[  2.   0.   0.   0.]
 [  0.   3.   0.   0.]
 [  0.   0.   1.   0.]
 [  0.  15.   0.   1.]]
####        Byte Representation :'\x00\x01TRANSFORM\x00\x00\x00RigidToTracker Test\x00\x00\x00\x00{t\xbcj~\x00\x00\x00\x00\x00\x00\x000\xf4uW:\xce\xf9\xb0>@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

########################

########################

#### Transform Message

####        Transform           :
[[ 2.  0.  0.  0.]
 [ 0.  3.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]
####        Byte Representation :'\x00\x01TRANSFORM\x00\x00\x00RigidToTracker Test\x00\x00\x00\x00{t\xbcj~\x00\x00\x00\x00\x00\x00\x000\xf4uW:\xce\xf9\xb0>@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

########################


In [9]:
status = OpenIGTLink.OpenIGTLinkStatus()
message = status.setOpenIGTLinkStatus(statusCode = 9,\
                                         statusSubCode = 254,\
                                         errorName = 'Example Error Name',\
                                         statusMessage = None,\
                                         deviceName = 'Tracker',\
                                         floatTimeStamp = 123.456)
print status

statusTest = OpenIGTLink.OpenIGTLinkStatus()
statusTest.header.unpack(message[0:transformTest.header.IGTLinkHeaderSize])
statusTest.unpackStatus(message[transformTest.header.IGTLinkHeaderSize::])
print statusTest


########################

#### Status Message

####        Status Code         :9
####        Status Subcode      :254
####        Error Name          :Example Error Name
####        Status Message      :Checksum error
####        Byte Representation :'\x00\x01STATUS\x00\x00\x00\x00\x00\x00Tracker\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00{t\xbcj~\x00\x00\x00\x00\x00\x00\x00,\xc2\xb8r\xad\xf2\x07\x0eA\x00\t\x00\x00\x00\x00\x00\x00\x00\xfeExample Error Name\x00\x00Checksum error'

########################

########################

#### Status Message

####        Status Code         :9
####        Status Subcode      :254
####        Error Name          :Example Error Name
####        Status Message      :Checksum error
####        Byte Representation :'\x00\x01STATUS\x00\x00\x00\x00\x00\x00Tracker\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00{t\xbcj~\x00\x00\x00\x00\x00\x00\x00,\xc2\xb8r\xad\xf2\x07\x0eA\x00\t\x00\x00\x00\x00\x00\x00\x00\xfeExample Error Name\x00\x00Checksum error'

########################

Read Real Messages and decode


In [10]:
import struct
messagefile = os.path.join(os.path.join(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)),\
                                             'OpenIGTLink'),\
                                             'ExampleFiles'),\
                                             'ExampleMessages.txt')

text_file = open(messagefile, "r")
messages = text_file.read()
text_file.close()

firstMessage = messages[0:106]
a =  repr(firstMessage)

transform = OpenIGTLink.OpenIGTLinkTransform()
transform.header.unpack(firstMessage[0:transform.header.IGTLinkHeaderSize])
transform.unpackTransform(firstMessage[transform.header.IGTLinkHeaderSize::])
print 'Byte Representation equal: ', repr(transform.getMessageToSend()) == repr(firstMessage)


Byte Representation equal:  True

Read Message File


In [11]:
import pandas
messagefile = os.path.join(os.path.join(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)),\
                                             'OpenIGTLink'),\
                                             'ExampleFiles'),\
                                             'Model.csv')
df = pandas.read_csv(messagefile)
df


Out[11]:
TYPE DEVICE_NAME TIME_STAMP T00 T01 T02 T03 T10 T11 T12 T13 T20 T21 T22 T23 CODE SUBCODE ERROR_NAME STATUS_MESSAGE
0 TRANSFORM RigidToTracker 0 1 2 3 4 5 6 7 8 9 10 11 12 NaN NaN NaN NaN
1 STATUS My Device 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1 0 OK This is a status message

In [12]:
messageList = list()
for i in range(len(df.index)):
    message = df.loc[i]
    
    m = None
    if message['TYPE'] == 'TRANSFORM':
        
        npTransform = numpy.eye(4)
        for ii in range(3):
            for jj in range(4):
                npTransform[ii,jj] = float(message['T' + str(ii) + str(jj)])
        
        
        t = OpenIGTLink.OpenIGTLinkTransform()
        m = t.setOpenIGTLinkTransform(npTransform = npTransform,\
            floatTimeStamp = float(message['TIME_STAMP']),\
            transformName = message['DEVICE_NAME'])
        messageList.append(m)
    
    elif message['TYPE'] == 'STATUS':
        s = OpenIGTLink.OpenIGTLinkStatus()
        m = s.setOpenIGTLinkStatus(statusCode = int(message['CODE']),\
                                         statusSubCode = int(message['SUBCODE']),\
                                         errorName = message['ERROR_NAME'],\
                                         statusMessage = message['STATUS_MESSAGE'],\
                                         deviceName = message['DEVICE_NAME'],\
                                         floatTimeStamp = float(message['TIME_STAMP']))
        messageList.append(m)
    else:
        print "Problem realind row: ", i
    
for i, m in enumerate(messageList):
    print '[{0:d}]'.format(i), m


[0] TRANSFORMRigidToTracker0K����?�@�A@@�A @@@�A0@�AA@
[1] STATUSMy Device6�GJ��_xOKThis is a status message

Create and launch server

  1. Run this cell in a different ipython session
  2. Run the second cell for receive the data in other ipython session

In [ ]:
%load_ext autoreload
%autoreload 2
import os
import sys
import time
# Get current Directory
wd = os.getcwd()
# Add package to path
sys.path.append(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)), 'OpenIGTLink'))

import OpenIGTLink

messagefile = os.path.join(os.path.join(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)),\
                                             'OpenIGTLink'),\
                                             'ExampleFiles'),\
                                             'Model.csv')
server = OpenIGTLink.OpenIGTLinkServer()
server.addMessageFile(messagefile, 'File #1')
server.connect()
time.sleep(3)
server.sendData(rate = 1.0)

server.disconnect()

In [ ]:
%load_ext autoreload
%autoreload 2
import os
import sys
import time
# Get current Directory
wd = os.getcwd()
# Add package to path
sys.path.append(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)), 'OpenIGTLink'))
import OpenIGTLink

messagefileReceived = os.path.join(os.path.join(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)),\
                                             'OpenIGTLink'),\
                                             'ExampleFiles'),\
                                             'ModelReceived.csv')

client = OpenIGTLink.OpenIGTLinkClient()
client.connect()
time.sleep(2)
client.listenData()
time.sleep(5)
client.disconnect()
client.writeFile(messagefileReceived)